package login;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.Vector;

public class Login {
	private String mid="";
	private String pwd="";
	private String newPwd1="";
	private String address="";
	private String birth="";
	private String phone="";
	private String email="";
	private String question="";
	private String answer="";
	private Date nal;
	private String addr1 = "";
	private String addr2 = "";
	private int num = 0;
	LoginVo vo;


	String driver="oracle.jdbc.driver.OracleDriver";
	String url="jdbc:oracle:thin:@192.168.10.108:1521:xe";
	String db_user="hr";
	String db_pwd="hr";
	Connection conn;
	public Login(){

		try{
			//1)드라이버 로딩
			Class.forName(driver).newInstance();	
			//2)커넥션 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);	
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public boolean login(){//
		boolean flag=false;
		try{
			String sql ="select * from join where mid=? and pwd=?";
			//3)PreparedStatement 작성
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1,mid);
			ps.setString(2,pwd);
			//4)리절트 셋 생성
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				flag=true;

			}else{
				flag=false;
			}
			ps.close();
			conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return flag;
	}

	public boolean insert(){
		boolean flag = false;
		address = addr1 + "%" + addr2;
		String sql = "insert into join values(?,?,?,?,?,?,?,sysdate)"; 
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, pwd);
			ps.setString(3, address);
			ps.setString(4, phone);
			ps.setString(5, email);
			ps.setString(6, question);
			ps.setString(7, answer);

			int cnt = ps.executeUpdate();
			if(cnt>0) flag = true;

			ps.close();
			conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}

		return flag;
	}
	
	public boolean id_check(){
		boolean flag = false;	
		
		String sql = "select mid from join where mid=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				flag = true;	
			}
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}	
		return flag;
	}
	
	public LoginVo select(){
		String sql = " select * from join where mid = ?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ResultSet rs = ps.executeQuery();
		
			if(rs.next()){	
				vo = new LoginVo(rs.getString("mid"), rs.getString("pwd"));			
				vo.setAddress(rs.getString("address"));
				vo.setEmail(rs.getString("email"));
				vo.setPhone(rs.getString("phone"));
				vo.setAnswer(rs.getString("answer"));
		
			}
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception ex){

		}
		return vo;
	}
	
	public LoginVo search_id(){
		String sql = " select * from join where email=? and question=? and answer=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, email);
			ps.setString(2, question);
			ps.setString(3, answer);
			ResultSet rs = ps.executeQuery();
		
			if(rs.next()){	
				vo = new LoginVo(rs.getString("mid"), rs.getString("pwd"));					
			}
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception ex){

		}
		return vo;
	}
	
	public LoginVo search_pwd(){
		String sql = " select * from join where mid=? and email=? ";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, email);
			ResultSet rs = ps.executeQuery();
		
			if(rs.next()){	
				vo = new LoginVo(rs.getString("mid"), rs.getString("pwd"));		
				vo.setEmail(rs.getString("email"));
			}
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception ex){

		}
		return vo;
	}

	//public Vector<LoginVo> list(){
	//	Vector<LoginVo> v = new Vector<>(); 
	//	String sql ="select * from member where irum like ?";
	//	try{
	//		PreparedStatement ps = conn.prepareStatement(sql);
	//		ps.setString(1, "%"+mid+"%");
	//		ResultSet rs = ps.executeQuery();
	//		
	//		while(rs.next()){
	//			LoginVo m =new LoginVo(rs.getString("irum"),rs.getString("pwd"));
	//			v.add(m);
	//		}
	//	rs.close();
	//	ps.close();
	//	conn.close();
	//	}catch(Exception ex){
	//		ex.printStackTrace();
	//	}
	//	
	//	return v;
	//}


	//public LoginVo view(){
	//	LoginVo m=null;
	//	String sql = "select * from member where irum=? ";
	//	try{
	//		PreparedStatement ps = conn.prepareStatement(sql);
	//		ps.setString(1,mid);
	//		ResultSet rs = ps.executeQuery();
	//		
	//		if(rs.next()){
	//			m = new LoginVo(rs.getString("irum"), rs.getString("pwd"));
	//		
	//		}
	//		
	//	}catch(Exception ex){
	//		ex.printStackTrace();
	//	}
	//		
	//	return m;
	//	
	//}
	public boolean modify(){
		boolean flag = false;
		String sql = "update join set pwd=?,address=?,phone=?,email=?,question=?,answer=? where mid=?"; 
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, pwd);
			ps.setString(2, address);
			ps.setString(3, phone);
			ps.setString(4, email);
			ps.setString(5, question);
			ps.setString(6, answer);
			ps.setString(7, mid);

			int cnt = ps.executeUpdate();
			if(cnt>0) flag = true;
			
			ps.close();
			conn.close();
		}catch(Exception ex){

		}

		return flag;
	}
	
	public boolean delete(){
		boolean flag = false;

		String sql = "delete from join where mid=? and pwd = ?"; 
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, pwd);
			
			int cnt = ps.executeUpdate();
			if(cnt>0) flag = true;
		}catch(Exception ex){
			ex.printStackTrace();
		}

		return flag;
	}


	public String getMid() {
		return mid;
	}
	public void setMid(String mid) {
		this.mid = mid;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getNewPwd1() {
		return newPwd1;
	}
	public void setNewPwd1(String newPwd1) {
		this.newPwd1 = newPwd1;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getBirth() {
		return birth;
	}
	public void setBirth(String birth) {
		this.birth = birth;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public Date getNal() {
		return nal;
	}
	public void setNal(Date nal) {
		this.nal = nal;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public String getAddr1() {
		return addr1;
	}
	public void setAddr1(String addr1) {
		this.addr1 = addr1;
	}
	public String getAddr2() {
		return addr2;
	}
	public void setAddr2(String addr2) {
		this.addr2 = addr2;
	}

	
	
	
}